
#include <GL/glu.h>
#include "glDrawFunctions.h"

void glDrawSkeletonBones(GSkeleton *skeleton) {
   
   if (skeleton==0) return;
   
   glDisable(GL_LIGHTING);
   glLineWidth(4.0);
   glPointSize(5.0);
   for (int i=0; i<skeleton->nBones(); ++i) {
      Segment3 b = skeleton->getBone(i);
      const Point3 &source = b.source();
      const Point3 &target = b.target();
      glBegin(GL_LINES);
         glColor3d(0.0, 0.0, 0.0);
         glVertex3d(source.x(), source.y(), source.z());
         glVertex3d(target.x(), target.y(), target.z());
      glEnd();
      glBegin(GL_POINTS);
         glColor3d(1.0, 0.0, 0.0);
         glVertex3d(source.x(), source.y(), source.z());
         glVertex3d(target.x(), target.y(), target.z());
      glEnd();
   }
   glEnable(GL_LIGHTING);
}

/// Draws a 3D cursor
void drawCursor3D(const Point3 &c, Number sf) {
   
   glDisable(GL_LIGHTING);
   glColor4f(1.0,0.5,0.0,1.0);
   glLineWidth(1.0);
   
   glBegin(GL_LINE_STRIP);
   Vector3 V(-1,0,0); Vector3 uV(0,1,0); /// V = crossproduct(Vector3(0,0,1),uV)
   for (float t=0; t<2.0*3.2; t+=0.1) {
      Point3 p = c + sf*cos(t)*uV + sf*sin(t)*V;
      glVertex3f(p[0], p[1], p[2]);
   }
   glEnd();
   
   Point3 p0 = c + 0.5*sf*Vector3(1,0,0);
   Point3 p1 = c + 1.5*sf*Vector3(1,0,0);
   Point3 p2 = c + 0.5*sf*Vector3(0,1,0);
   Point3 p3 = c + 1.5*sf*Vector3(0,1,0);
   Point3 p4 = c + 0.5*sf*Vector3(-1,0,0);
   Point3 p5 = c + 1.5*sf*Vector3(-1,0,0);
   Point3 p6 = c + 0.5*sf*Vector3(0,-1,0);
   Point3 p7 = c + 1.5*sf*Vector3(0,-1,0);
   
   glColor4f(1.0,0.0,0.0,1.0);
   glBegin(GL_LINES);
   glVertex3f(p0[0], p0[1], p0[2]);
   glVertex3f(p1[0], p1[1], p1[2]);
   glVertex3f(p2[0], p2[1], p2[2]);
   glVertex3f(p3[0], p3[1], p3[2]);
   glVertex3f(p4[0], p4[1], p4[2]);
   glVertex3f(p5[0], p5[1], p5[2]);
   glVertex3f(p6[0], p6[1], p6[2]);
   glVertex3f(p7[0], p7[1], p7[2]);
   glEnd();
   
   glEnable(GL_LIGHTING);
}
